﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using mesoBoard.Data;
using mesoBoard.Data.Repositories;
using System.Drawing.Imaging;
using System.Drawing;
using System.Web.Mvc;

namespace mesoBoard.Services
{
    public class Themes : IThemes
    {
        private IRepositoriesWrapper Repositories;

        public Themes(IRepositoriesWrapper repos)
        {
            this.Repositories = repos;
        }

        public Theme GetTheme(User CurrentUser, string controllerName, string previewTheme)
        {
            Theme TheTheme;

            if (controllerName == "Admin")
                TheTheme = Repositories.Themes.GetThemes().FirstOrDefault(x => x.FolderName == "Default");
            else if (previewTheme != null)
                TheTheme = Repositories.Themes.GetTheme(int.Parse(previewTheme));
            else if (bool.Parse(SiteConfig.OverrideUserTheme.Value) || CurrentUser == null || CurrentUser.UserID == 0)
                TheTheme = Repositories.Themes.GetTheme(int.Parse(SiteConfig.BoardTheme.Value));
            else
                TheTheme = Repositories.Themes.GetUserTheme(CurrentUser.UserID);

            return TheTheme;
        }

        public IEnumerable<Theme> GetVisibleThemes()
        {
            return Repositories.Themes.GetThemes().Where(y => y.VisibleToUsers == true);
        }

        public void ChangeDefaultTheme(int themeID)
        {
            Repositories.Configs.UpdateConfig("BoardTheme", themeID.ToString());
            SiteConfig.UpdateCache();
        }
    }
}